package com.huanhuan.doc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huanhuan.beans.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

public class ESTest_Client_doc_Update {
    public static void main(String[] args) throws Exception{
        final String hostname = "localhost";
        final int port = 9200;
        final String scheme = "http";
        HttpHost httphost = new HttpHost(hostname, port,scheme);
        //创建环境
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder( httphost )
        );

        //修改数据
        UpdateRequest updateRequest = new UpdateRequest();
        //添加文档的索引和文档的id
        updateRequest.index("user").id("1001");
        updateRequest.doc(XContentType.JSON,"age","18");


        //修改文档
        UpdateResponse updateResponse = client.update( updateRequest, RequestOptions.DEFAULT );
        System.out.println( updateResponse.getResult() );


        client.close();

    }
}
